#include <iostream>
#include <curl/curl.h>
#include <nlohmann/json.hpp>
using namespace std;
using json=nlohmann::json;

 
static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
{
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}
 
int main()
{
    CURL *curl;
    CURLcode res;
    std::string readBuffer;
    // 初始化模块
    curl_global_init(CURL_GLOBAL_DEFAULT);
    // 初始化请求库
    curl = curl_easy_init();
    if(curl)
    {
        // CURLOPT_URL 请求的网站
        curl_easy_setopt(curl, CURLOPT_URL, "http://httpbin.org/get");
        // CURLOPT_WRITEFUNCTION 设置回调函数
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer);
        // 执行CURL访问网站
        res = curl_easy_perform(curl);
        if(res != CURLE_OK) {
            fprintf(stderr, "curl_easy_perform() failed: %s\n", curl_easy_strerror(res));
        } else {
            // std::cout << readBuffer << std::endl;
        }
 
        curl_easy_cleanup(curl);
    }
    curl_global_cleanup();
    

    json jsonobj=json::parse(readBuffer);
    //delete
    jsonobj["headers"].erase("Host");
    //update
    jsonobj["url"]="https://baidu.com";
    //add
    jsonobj["dd"]="hello world!!!";
    std::cout << jsonobj << endl;

    return 0;
}